package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.rjwh.dingdong.client.bean.jsonbean.ResGetMainPageBean;
import com.rjwh.dingdong.client.bean.localbean.GetMainPageBean;
import com.rjwh.dingdong.client.bean.localbean.MainPageModuleData;
import com.rjwh.dingdong.client.bean.localbean.PictureBookMainPageBannerData;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.MainPageModel;
import com.rjwh.dingdong.client.mvp.presenter.IMainPagePresenter;
import com.rjwh.dingdong.client.mvp.view.MainPageView;

import java.util.List;

/**
 * @author Kay
 * @ClassName MainPagePresenter
 * @Description
 * @date 2016-10-26
 * @history 2016-10-26 author: description:
 */

public class MainPagePresenter extends BasePresenter<MainPageView> implements IMainPagePresenter {

    private MainPageModel model;

    public MainPagePresenter(Context context, MainPageView mMvpView) {
        super(context, mMvpView);
        model = new MainPageModel(this);
    }

    /**
     * 主页
     */
    @Override
    public void doGetMainPage() {
        if (isViewAttached()) {
            model.doGetMainPage();
        }
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (getMvpView() != null) {
            getMvpView().hideLoadingView();
            switch (businessType) {
                case NetConstant.GETMAINPAGE:
                    getMvpView().setRefreshing(false);
                    ResGetMainPageBean resGetMainPageBean = (ResGetMainPageBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && resGetMainPageBean != null && resGetMainPageBean.getData() != null) {
                        getMvpView().hideErrorView();
                        GetMainPageBean data = resGetMainPageBean.getData();
                        List<PictureBookMainPageBannerData> bannerlist = data.getBannerlist();
                        List<MainPageModuleData> modulelist = data.getModulelist();
                        if (bannerlist != null && modulelist != null) {
                            getMvpView().LoadData(bannerlist, modulelist);
                        } else {
                            getMvpView().showErrorView();
                        }
                    } else {
                        getMvpView().showErrorView();
                    }
                    break;
                default:
                    break;
            }
        }
    }

    @Override
    public void detachView() {
        model.detachView();
        detachViews();
    }

}
